//
//  GDTrainStationController.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/7.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDTrainStationController.h"
#import "MBProgressHUD+MJ.h"
#import "GDTansitionModelDateOperation.h"
#import "GDTrainInformationModel.h"
#import "GDTrainViewModel.h"
#import "GDShowTrainsDataController.h"
#import <iAd/iAd.h>
@interface GDTrainStationController ()<ADBannerViewDelegate>{
    ADBannerView *iadBannerView;
}

@property(nonatomic,weak) UIDatePicker * datePick;
@property (weak, nonatomic) IBOutlet UIButton *btnStartTime;
@property (weak, nonatomic) IBOutlet UIButton *btnSearch;
- (IBAction)btnTransitionClick:(id)sender;
- (IBAction)btnSearchClickBack:(id)sender;
- (IBAction)btnDateClick:(id)sender;
@property (weak, nonatomic) IBOutlet UITextView *txtShowNotice;
@property (nonatomic,strong) NSArray * trainsArrayModel;


@end

@implementation GDTrainStationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.navigationItem.title = @"乐趣出行";
//    UIBarButtonItem *backToHomeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"left-cross"] style:UIBarButtonItemStylePlain target:self action:@selector(backToHomeClick)];
////    UIBarButtonItem * backToHomeItem = [[UIBarButtonItem alloc] initWithTitle:@"<<<" style:UIBarButtonItemStylePlain target:self action:@selector(backToHomeClick)];
//    self.navigationItem.leftBarButtonItem = backToHomeItem;
    [self setNavigatorControl];
    self.startName.frame =  CGRectMake(20, 20, 130, 50);

    
}

- (void) setNavigatorControl
{
    CGRect aScreenRect = [[UIScreen mainScreen] bounds];
    NSString *nvTitle = @"乐趣出行";
    
    //创建navbar
    UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, aScreenRect.size.width, 66)];
    //创建navbaritem
    UINavigationItem *NavTitle =  [[UINavigationItem alloc] initWithTitle: nvTitle];
 
    [nav pushNavigationItem:NavTitle animated:YES];
    [self.view addSubview:nav];
    //创建barbutton 创建系统样式的
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"left-cross"] style:UIBarButtonItemStylePlain target:self action:@selector(rtnExit)];
    
    
    //设置barbutton
    NavTitle.leftBarButtonItem = backItem;
    [nav setItems:[NSArray arrayWithObject:NavTitle]];
}

- (void) rtnExit{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    iadBannerView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, GDScreenSize.size.height - 44, GDScreenSize.size.width, 44)];
    iadBannerView.delegate = self;
    [self.view addSubview:iadBannerView];
    
}

#pragma mark 广告加载成功
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
    NSLog(@"%s",__func__);
}

#pragma mark 广告加载失败
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
    NSLog(@"%s\t %@",__func__,error);
}

//- (void) backToHomeClick{
//    
//    [self dismissViewControllerAnimated:YES completion:nil];
//}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [NSTimer initialize];
}


#pragma mark *******************出发站、终点站互换按钮*******************
- (IBAction)btnTransitionClick:(id)sender {
    
    if(self.startName.text == nil  || self.endName.text == nil){
        return;
    }
    NSString * tempStr = self.startName.text;
    self.startName.text = self.endName.text;
    self.endName.text = tempStr;

}

#pragma mark *******************查询按钮*******************
- (IBAction)btnSearchClickBack:(id)sender {

    [self.view endEditing:YES]; // 关闭键盘

    if (self.startName.text == nil || self.endName.text == nil || self.startName.text.length == 0
        || self.endName.text.length == 0) {
        ShowErrorAlert(@"请按要求填写");
        return;
    }
    [self getTrainsModel];
}

#pragma mark *******************日期选择按钮*******************
- (IBAction)btnDateClick:(id)sender {
    
    UIDatePicker * datePick = [[UIDatePicker alloc] init];
    _datePick = datePick;
    datePick.datePickerMode = UIDatePickerModeDate;
    datePick.frame = CGRectMake((GDScreenSize.size.width - 250 )/2, GDScreenSize.size.height * 0.5 - 75, 250, 150);
    UIView * tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GDScreenSize.size.width, GDScreenSize.size.height)];
    tempView.backgroundColor = [UIColor grayColor];
    tempView.alpha = 0.6;
    [UIView animateWithDuration:1 animations:^{
        [self.view addSubview:tempView];
        [self.view addSubview:datePick];
    }];
    tempView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tempViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getDatePickDateAndRemoveIt:)];
    [tempView addGestureRecognizer:tempViewTap];
}

- (void) getDatePickDateAndRemoveIt: (UITapGestureRecognizer *) tapGes {

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    [self.btnStartTime setTitle:[dateFormatter stringFromDate:self.datePick.date] forState:UIControlStateNormal];
    self.startTime = [dateFormatter stringFromDate:self.datePick.date];
    [tapGes.view removeFromSuperview];
    [self.datePick removeFromSuperview];
}

-(void) getTrainsModel{
    [MBProgressHUD showMessage:@"正在获取数据中,请稍后..." toView:self.view];
    [GDTansitionModelDateOperation initDateWithTrainStation:self success:^(NSArray *model) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if(model){
            self.trainsArrayModel = model;
            [self updateUI:model];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error] toView:self.view];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 更新UI
-(void) updateUI:(NSArray *) arrayModel{
    /**
     *  此处要获取到模型的具体高度（在调用uitableView之前）
     */
    GDShowTrainsDataController * showTrainsDataController = [[GDShowTrainsDataController alloc] init];
    showTrainsDataController.trainsArrayModel = self.trainsArrayModel;
//    [self.navigationController pushViewController:showTrainsDataController animated:YES];
    [self presentViewController:showTrainsDataController animated:YES completion:nil];

}

#pragma mark *******************显示广告*******************

-(BOOL)prefersStatusBarHidden{
    return YES;
}


@end
